
#region Imports

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Justa.Web.Generic;

#endregion

namespace Justa.Web.Control.Theme
{
    /// <summary>
    /// 
    /// </summary>
    /// 
    /// <author>Abel Perez (java.aperez@gmail.com)</author>
    /// 
    [
    AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal),
    DefaultProperty("DataThemeField"), ToolboxData("<{0}:ThemeSwatch runat=\"server\"> </{0}:ThemeSwatch>")
    ]
    public class ThemeSwatch : DataBoundControl
    {
        /// <summary>
        /// 
        /// </summary>
        /// 
        private static readonly string DefaultImageType = ".gif";

        /// <summary>
        /// 
        /// </summary>
        /// 
        private static readonly string DefaultImagePath = "/Images/";

        /// <summary>
        /// 
        /// </summary>
        /// 
        private PersistableCollection<ThemeOption> items;

        /// <summary>
        /// 
        /// </summary>
        /// 
        public event EventHandler OnClick;

        /// <summary>
        /// 
        /// </summary>
        /// 
        public virtual PersistableCollection<ThemeOption> Items
        {
            get
            {
                if (items == null)
                {
                    items = new PersistableCollection<ThemeOption>();
                }

                return items;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        public virtual string DataThemeField
        {
            get
            {
                object result = ViewState["DataThemeField"];
                if (result == null)
                {
                    return String.Empty;
                }

                return result as string;
            }

            set
            {
                ViewState["DataThemeField"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        public virtual string DataIdField
        {
            get
            {
                object result = ViewState["DataIdField"];
                if (result == null)
                {
                    return String.Empty;
                }

                return result as string;
            }

            set
            {
                ViewState["DataIdField"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        public virtual string DataImageTypeField
        {
            get
            {
                object result = ViewState["DataImageTypeField"];
                if (result == null)
                {
                    return DefaultImageType;
                }

                return result as string;
            }

            set
            {
                ViewState["DataImageTypeField"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        public virtual string DataImagePathField
        {
            get
            {
                object result = ViewState["DataImagePathField"];
                if (result == null)
                {
                    return DefaultImagePath;
                }

                return result as string;
            }

            set
            {
                ViewState["DataImagePathField"] = value;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// 
        /// <param name="dataSource"></param>
        /// 
        protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
        {
            base.PerformDataBinding(dataSource);

            string themeName = DataThemeField;
            string themeControlId = DataIdField;

            if (dataSource != null)
            {
                foreach (object item in dataSource)
                {
                    ThemeOption themeOption = new ThemeOption();
                    themeOption.Name = DataBinder.GetPropertyValue(item, themeName, null);
                    themeOption.Id = DataBinder.GetPropertyValue(item, themeControlId, null);
                    Items.Add(themeOption);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        /// <param name="writer"></param>
        /// 
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            foreach (ThemeOption themeOption in Items)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Format("{0}Theme", themeOption.Name));
                writer.AddAttribute(HtmlTextWriterAttribute.Href, GetThemeUrl(themeOption));
                writer.RenderBeginTag(HtmlTextWriterTag.A);

                writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Format("{0}Image", themeOption.Name));
                //writer.AddAttribute(HtmlTextWriterAttribute.Src, string.Format("{0}{1}{2}_option{3}", Page.Request.ApplicationPath, DefaultImagePath, themeOption.Name, DefaultImageType));
                writer.AddAttribute(HtmlTextWriterAttribute.Src, string.Format("{0}{1}_option{2}", DefaultImagePath, themeOption.Name, DefaultImageType));
                writer.RenderBeginTag(HtmlTextWriterTag.Img);

                writer.RenderEndTag();
                writer.RenderEndTag();
                writer.Write("&nbsp;");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        /// <param name="themeOption"></param>
        /// 
        /// <returns></returns>
        /// 
        protected string GetThemeUrl(ThemeOption themeOption)
        {
            //return string.Format("{0}/?id={1}&theme=css/{2}.css", Page.Request.ApplicationPath, themeOption.Id, themeOption.Name);
            return string.Format("/?id={0}&theme=css/{1}.css", themeOption.Id, themeOption.Name);
        }
    }
}